package com.dinzeer.legendreliclib.event;

import com.dinzeer.legendreliclib.configs.Config;
import com.dinzeer.legendreliclib.lib.compat.slashblade.SlashEffect;
import com.dinzeer.legendreliclib.lib.compat.slashblade.SwordRainGenerator;
import com.dinzeer.legendreliclib.lib.tags.SlashBladeCompatTag;
import com.dinzeer.legendreliclib.lib.util.WaitingTick;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SlashBladeCompat {
    // 添加递归深度计数器
    private static final ThreadLocal<Integer> recursionDepth = ThreadLocal.withInitial(() -> 0);
    private static final int MAX_RECURSION_DEPTH = 3;
    private static final Logger LOGGER = LogManager.getLogger();

    // 修复：添加@SubscribeEvent注解确保事件注册
    @SubscribeEvent
    public static void DoslashBlade(SlashBladeEvent.DoSlashEvent event) {
//        Summoners.SwordSpiritUtils.summonSwordSpirit(event.getUser(),600,10);
//
//

//        SwordRainGenerator.generateFivePointSwordRain(event.getUser(), event.getUser().level(),5);








        if (!Config.CanDoubleAttack.get())return;
        int currentDepth = recursionDepth.get();
        if (currentDepth >= MAX_RECURSION_DEPTH) {
            LOGGER.warn("达到最大递归深度({})，终止处理以避免无限循环", MAX_RECURSION_DEPTH);
            return;
        }

        recursionDepth.set(currentDepth + 1);
        try {
            if (event.getUser() != null) {
                if (event.getUser().getOffhandItem().is(SlashBladeCompatTag.CAN_DOUBLE_ATTACK)) {
                    WaitingTick.schedule(2, () -> {
                        SlashEffect.SakuraEnd.doSlash(event.getUser(), event.getRoll() * -1, SlashBladeUtil.getColorCode(event.getUser().getOffhandItem()),
                                Vec3.ZERO, true, true, event.getDamage(), event.getKnockback());
                    });

                }
            }
        } finally {
            recursionDepth.set(currentDepth);
        }
    }
}
